﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class ChucVuManager
    {
        public ActionResultAsList<ChucVu> LayDS()
        {
            var ar = new ActionResultAsList<ChucVu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.ChucVus.Include("NhanViens").ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<ChucVu> LayDSTheoMaCV(string maCV)
        {
            var ar = new ActionResultAsList<ChucVu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.ChucVus.Include("NhanViens").Where(c => c.MaChucVu == maCV).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<ChucVu> LayDSTheoTenCV(string tenCV)
        {
            var ar = new ActionResultAsList<ChucVu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.ChucVus.Include("NhanViens").Where(c => c.TenChucVu == tenCV).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult Them(ChucVu cv)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    List<ChucVu> list = db.ChucVus.ToList();
                    if (list.Exists(c => c.MaChucVu == cv.MaChucVu))
                    {
                        ac.AddException(new Exception("Mã chức vụ đã tồn tại: " + cv.MaChucVu));
                        return ac;
                    }
                    db.ChucVus.Add(cv);
                    db.SaveChanges();
                    cv = db.ChucVus.Include("Nhanviens").Single(c => c.MaChucVu == cv.MaChucVu);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult CapNhat(ChucVu cv)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var cv1 = db.ChucVus.Single(c => c.MaChucVu == cv.MaChucVu);
                    cv1.MoTa = cv.MoTa;
                    cv1.TenChucVu = cv.TenChucVu;
                    db.SaveChanges();
                    cv = db.ChucVus.Include("Nhanviens").Single(c => c.MaChucVu == cv.MaChucVu);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult Xoa(string maCV)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var cv = db.ChucVus.Single(c => c.MaChucVu == maCV);
                    db.ChucVus.Remove(cv);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }
    }
}
